#include "sgu.h"

SGU_NS_BEGIN

unsigned int powerof2(int n)
{
	if(n<0) return 0;
	return 1 << n;
}

bool ispowerof2(unsigned int n)
{
	while(n>1)
	{
		if(n&1) return false;
		n = n>>1;
	}
	return true;
}

unsigned int nextpowerof2(unsigned int n)
{
	bool a;
	int k = log2inf(n,&a);
	if(!a) ++k;
	return powerof2(k);
}
	



int log2inf(unsigned int n, bool * exact)
{
	if(exact) *exact = true;
	if(n==0) return -1;
	int c = 0;	
	while(n>1)
	{
		if(n&1)	if(exact) *exact = false;
		n = n>>1;
		++c;
	}
	return c;
}	


SGU_NS_END
